package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.im.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    QuestionService questionService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    @DubboReference
    FriendService friendService;

    @DubboReference
    CommentService commentService;

    public void replyStrangerQuestion(Long recommendUserId, String reply) {
        //1. 获取登陆者id
        Long userId = UserHolder.get().getId();

        //2.调用service获取当前用户的userInfo
        UserInfo userInfo = userInfoService.findById(userId);

        //3.调用service查询对方的Question
        Question question = questionService.findByUserId(recommendUserId);
        //注意:对方有可能没有设置问题,若没有就使用默认问题
        if (question == null) {
            question = new Question();
        }

        //4.调用环信模板中的发送信息方法
        /*
        userId:登陆者id
        huanXinId:登陆者环信id
        nickname:登陆者昵称
        strangerQuestion:推荐用户的陌生人问题
        reply:登陆者回复的内容
         */
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("huanXinId","HX"+userId);
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion",question.getStrangerQuestion());
        map.put("reply",reply);

        huanXinTemplate.sendMsg("HX"+recommendUserId, JSON.toJSONString(map));
    }

    public void addContact(Long friendId) {
        Long userId = UserHolder.get().getId();
        //1.调用service添加好友
        friendService.addContact(userId,friendId);

        //2.调用环信模板添加好友
        huanXinTemplate.addContacts("HX"+userId,"HX"+friendId);
    }

    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        //1.获取登陆者id
        //2.调用service查询当前页的好友数据
        PageBeanVo pageBeanVo = friendService.findContactsByPage(UserHolder.get().getId(), pageNum, pageSize);

        //3.从pageBeanVo中获取好友list
        List<Friend> items = (List<Friend>) pageBeanVo.getItems();

        //4.遍历好友list,获取每个好友id,查询好友的UserInfo,将userInfo和环信id封装ContactVo(注意:userId是环信id),将vo放入预先声明的voList中
        List<ContactVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(friend -> {
                //查询好友的UserInfo
                UserInfo userInfo = userInfoService.findById(friend.getFriendId());

                //将userInfo和环信id封装ContactVo(
                ContactVo vo = new ContactVo();
                //先封装userInfo再封装环信id
                vo.setUserInfo(userInfo);
                // 千万注意:环信id
                vo.setUserId("HX"+friend.getFriendId());

                voList.add(vo);
            });
        }

        //5.将voList赋值给pageBeanVo
        pageBeanVo.setItems(voList);

        //6.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findCommentsByPage(Integer pageNum, Integer pageSize, int commentType) {
        //1.获取登陆者id(发布人id)
        //2.调用service查询当前页的数据
        PageBeanVo pageBeanVo = commentService.findByPage(UserHolder.get().getId(), commentType, pageNum, pageSize);

        //3.获取pageBeanVo中的list(操作人id)
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        //4.获取每个操作人id,查询操作人userInfo,将每个操作人封装UserCommentVo对象,将vo放入预先声明的voList中
        List<UserCommentVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(comment -> {
                Long userId = comment.getUserId();
                UserInfo userInfo = userInfoService.findById(userId);

                UserCommentVo vo = new UserCommentVo();
                vo.setId(userId.toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                voList.add(vo);
            });
        }

        //5.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);
        //6.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }
}
